package com.xunlai.infra.permission.business.controller;

import com.xunlai.infra.permission.business.service.ScenicAreaService;
import com.xunlai.infra.permission.business.vo.scenic.*;
import com.xunlai.infra.permission.common.exception.ExceptionCodeEnum;
import com.xunlai.infra.permission.common.security.TenantUser;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import com.xunlai.infra.permission.common.vo.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liang
 * @date 8/23/23 5:09 PM
 */
@RestController
@RequestMapping("travel/manage/scenic")
public class ScenicAreaManageController {

    private ScenicAreaService scenicAreaService;


    @PostMapping("create")
    public BaseResponse<Long> createScenicArea(@RequestBody ScenicAreaCreateVo scenicAreaCreateVo){
        return BaseResponse.success(scenicAreaService.createScenicArea(scenicAreaCreateVo));
    }

    @PutMapping("update/{id}")
    public BaseResponse<Boolean> updateScenicArea(@PathVariable Long id ,@RequestBody ScenicAreaCreateVo scenicAreaCreateVo) {
        scenicAreaService.updateScenicArea(id,scenicAreaCreateVo);
        return BaseResponse.success(true);
    }

    @DeleteMapping("delete/{id}")
    public BaseResponse<Boolean> delete(@PathVariable Long id){
        scenicAreaService.deleteScenicArea(id);
        return BaseResponse.success(true);
    }

    @GetMapping("list")
    public BaseResponse<BasePageRespVo<ScenicAreaListVo>> listScenicArea(ScenicAreaQueryVo scenicAreaQueryVo,
                                                                         @RequestParam(required = false,defaultValue = "false") Boolean custom,
                                                                         @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                                         @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
        if(Boolean.TRUE.equals(custom)){
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Object principal = authentication.getPrincipal();
            if(principal instanceof TenantUser tenantUser){
                String id = tenantUser.getId();
                return BaseResponse.success(scenicAreaService.listScenicArea(Long.valueOf(id),scenicAreaQueryVo,pageNum,pageSize));
            }else {
                return BaseResponse.success(new BasePageRespVo<>(new ArrayList<>(0),pageNum,pageSize,0L,1));
            }
        }else {
            return BaseResponse.success(scenicAreaService.listScenicArea(scenicAreaQueryVo,pageNum,pageSize));
        }
    }

    @GetMapping("all")
    public BaseResponse<List<ScenicAreaListVo>> allScenicArea(@RequestParam(required = false,defaultValue = "false") Boolean custom){
        if(Boolean.TRUE.equals(custom)){
            //客户自定义的景区
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Object principal = authentication.getPrincipal();
            if(principal instanceof TenantUser tenantUser){
                String id = tenantUser.getId();
                return BaseResponse.success(scenicAreaService.listAllScenicArea(Long.valueOf(id)));
            }else {
                return BaseResponse.success(new ArrayList<>(0));
            }
        }else {
            return BaseResponse.success(scenicAreaService.listAllScenicArea());
        }
    }

    @GetMapping("province/all")
    public BaseResponse<List<ProvinceListVo>> listProvince(){
        return BaseResponse.success(scenicAreaService.listAllProvince());
    }

    @GetMapping("area/all")
    public BaseResponse<List<AreaListVo>> listArea(){
        return BaseResponse.success(scenicAreaService.listAllArea());
    }

    @Autowired
    public void setScenicAreaService(ScenicAreaService scenicAreaService) {
        this.scenicAreaService = scenicAreaService;
    }
}
